﻿using B.WMS.IWMSService.Warehousety;
using B.WMS.StandardReturn;
using B.WMS.WMSDtos.Snventory;
using B.WMS.WMSDtos.Warehousety;
using B.WMS.WMSEntities.GoodsEntities;
using B.WMS.WMSEntities.Snventory;
using B.WMS.WMSEntities.Warehousety;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Linq.Dynamic.Core;

namespace B.WMS.Snventory
{
    public class DetailServices : ApplicationService, IDetailServices
    {
        private readonly IRepository<DetailModel, Guid> _detailRepository;
        private readonly IRepository<Goods, Guid> _goodsRepository;
        private readonly IRepository<Units, Guid> _unitsRepository;
        private readonly IRepository<Goodstype, Guid> _goodstypeRepository;

        public DetailServices(
            IRepository<DetailModel, Guid> detailRepository,
            IRepository<Goods, Guid> goodsRepository,
            IRepository<Units, Guid> unitsRepository,
            IRepository<Goodstype, Guid> goodstypeRepository)
        {
            _detailRepository = detailRepository;
            _goodsRepository = goodsRepository;
            _unitsRepository = unitsRepository;
            _goodstypeRepository = goodstypeRepository;
        }

        public async Task<ApiResult<DetailModelDto>> AddDetail(CreateUpdateDetailDto input)
        {
            var detail = ObjectMapper.Map<CreateUpdateDetailDto, DetailModel>(input);
            await _detailRepository.InsertAsync(detail);
            return new ApiResult<DetailModelDto>() { Code = ApiCode.Success, ResultData = ObjectMapper.Map<DetailModel, DetailModelDto>(detail) };
        }

        public async Task<ApiResult<DetailModel>> DeleteDetail(string id)
        {
            var detail = await _detailRepository.GetAsync(x => x.GoodscodeNo == id);
            await _detailRepository.DeleteAsync(detail);
            return new ApiResult<DetailModel>() { Code = ApiCode.Success, ResultData = detail };
        }

        public async Task<List<DetailModelDto>> GetDetail()
        {
            // 使用联查获取商品和单位信息
            var details = await _detailRepository.GetQueryableAsync();
            var goodstypes = await _goodstypeRepository.GetQueryableAsync();
            var units = await _unitsRepository.GetQueryableAsync();
            var goods = await _goodsRepository.GetQueryableAsync();
           
            

            var link = from a in details
                         join b in goodstypes on a.Goodstypeid equals b.Goodstypecoding
                         join c in units on a.GoodsUnitId equals c.Unitscoding
                         select new DetailModelDto
                         {
                             GoodscodeNo=a.GoodscodeNo,
                             GoodscodeName=a.GoodscodeName,
                             Goodstypename=b.WGoodstypename,
                             Rqty = a.Rqty,
                             Ramout =a.Ramout,
                             Pqty = a.Pqty,
                             GUnitName=c.UnitName,
                             Dqty = a.Dqty,
                             Damount = a.Damount,
                             Dstatus = a.Dstatus,
                             Dprice = a.Dprice,
                             Dremark = a.Dremark,
                             
                         };
            return link.ToList();
            
        }

        

        public async Task<ApiPaging<DetailModelDto>> GetDetailPage(PageInput input)
        {
            var query = await _detailRepository.GetQueryableAsync();
            var totalData = query.Count();
            var pageDatas = query.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var pageDtos = ObjectMapper.Map<List<DetailModel>, List<DetailModelDto>>(pageDatas);
            var totalPage = (int)Math.Ceiling((double)totalData / input.PageSize);
            return new ApiPaging<DetailModelDto> 
            {
                Code = ApiCode.Success,
                TotalData = totalData,
                TotalPage = totalPage,
                PageDatas = pageDtos
            };
        }

        
      
    }
}
